package org.rambadger.command.representation;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.mongojack.Id;
import org.mongojack.MongoCollection;
import org.rambadger.command.validation.UrlIdentifier;

import javax.validation.constraints.NotNull;

import static org.rambadger.command.representation.Assignment.AssignmentId;

@MongoCollection(name = "submissions")
public class Submission {
    public static class SubmissionId {
        @UrlIdentifier @NotNull
        @JsonProperty String username;

        @JsonProperty int submissionId;

        SubmissionId() {}

        public SubmissionId(String username, int submissionId) {
            this.username = username;
            this.submissionId = submissionId;
        }

        public String getUsername() {
            return username;
        }

        public int getSubmissionId() {
            return submissionId;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof SubmissionId)) return false;

            SubmissionId that = (SubmissionId) o;

            if (submissionId != that.submissionId) return false;
            if (!username.equals(that.username)) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = username.hashCode();
            result = 31 * result + submissionId;
            return result;
        }
    }

    @Id @NotNull
    @JsonProperty SubmissionId submissionId;

    @NotNull
    @JsonProperty AssignmentId assignmentId;

    Submission() {}

    public Submission(String username, int submissionId, String platformId, String assignmentId) {
        this.submissionId = new SubmissionId(username, submissionId);
        this.assignmentId = new AssignmentId(platformId, assignmentId);
    }

    public SubmissionId getSubmissionId() {
        return submissionId;
    }

    public AssignmentId getAssignmentId() {
        return assignmentId;
    }
}
